package com.ruoyi.project.modules.processBranch.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.mapper.InterfaceInfoMapper;
import com.ruoyi.project.modules.interfaceinfo.mapper.VirtualUsageMapper;
import com.ruoyi.project.modules.processBranch.vo.ParamObject;
import com.ruoyi.project.modules.processBranchCondition.domain.ProcessBranchCondition;
import com.ruoyi.project.modules.processBranchCondition.mapper.ProcessBranchConditionMapper;
import com.ruoyi.project.modules.processBranchConditionDetail.domain.ProcessBranchConditionDetail;
import com.ruoyi.project.modules.processBranchConditionDetail.mapper.ProcessBranchConditionDetailMapper;
import com.ruoyi.project.modules.processBranchEnd.domain.ProcessBranchEnd;
import com.ruoyi.project.modules.processBranchEnd.mapper.ProcessBranchEndMapper;
import com.ruoyi.project.modules.virtualUsageParamSource.domain.VirtualUsageParamSource;
import com.ruoyi.project.modules.virtualUsageParamSource.mapper.VirtualUsageParamSourceMapper;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.processBranch.mapper.ProcessBranchMapper;
import com.ruoyi.project.modules.processBranch.domain.ProcessBranch;
import com.ruoyi.project.modules.processBranch.service.IProcessBranchService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 流程分支Service业务层处理
 *
 * @author smallrain
 * @date 2022-04-02
 */
@Service
public class ProcessBranchServiceImpl implements IProcessBranchService {
    public static Map<String, String> condition = new HashMap<>();
    static {
        condition.put("EQUAL", " 等于 ");
        condition.put("GREATER_THAN", " 大于 ");
        condition.put("LESS_THAN", " 小于 ");
        condition.put("NOT_EQUAL", " 不等于 ");
        condition.put("GREATER_EQUAL_THAN", " 大于等于 ");
        condition.put("LESS_EQUAL_THAN", " 小于等于 ");
    }
    @Resource
    private ProcessBranchMapper processBranchMapper;
    @Resource
    private ISysDictTypeService sysDictTypeService;

    /**
     * 查询流程分支
     *
     * @param processBranchId 流程分支主键
     * @return 流程分支
     */
    @Override
    public ProcessBranch selectProcessBranchByProcessBranchId(Long processBranchId) {
        return processBranchMapper.selectProcessBranchByProcessBranchId(processBranchId);
    }

    /**
     * 查询流程分支列表
     *
     * @param processBranch 流程分支
     * @return 流程分支
     */
    @Override
    public List<ProcessBranch> selectProcessBranchList(ProcessBranch processBranch) {
        return processBranchMapper.selectProcessBranchList(processBranch);
    }

    /**
     * 新增流程分支
     *
     * @param processBranch 流程分支
     * @return 结果
     */
    @Override
    public int insertProcessBranch(ProcessBranch processBranch) {
        return processBranchMapper.insertProcessBranch(processBranch);
    }

    /**
     * 修改流程分支
     *
     * @param processBranch 流程分支
     * @return 结果
     */
    @Override
    public int updateProcessBranch(ProcessBranch processBranch) {
        return processBranchMapper.updateProcessBranch(processBranch);
    }

    /**
     * 批量删除流程分支
     *
     * @param processBranchIds 需要删除的流程分支主键
     * @return 结果
     */
    @Override
    public int deleteProcessBranchByProcessBranchIds(String processBranchIds) {
        return processBranchMapper.deleteProcessBranchByProcessBranchIds(Convert.toStrArray(processBranchIds));
    }

    /**
     * 删除流程分支信息
     *
     * @param processBranchId 流程分支主键
     * @return 结果
     */
    @Override
    public int deleteProcessBranchByProcessBranchId(Long processBranchId) {
        return processBranchMapper.deleteProcessBranchByProcessBranchId(processBranchId);
    }

    @Resource
    private ProcessBranchEndMapper processBranchEndMapper;

    @Resource
    private VirtualUsageParamSourceMapper virtualUsageParamSourceMapper;

    @Resource
    private VirtualUsageMapper virtualUsageMapper;

    @Override
    public synchronized void saveOrUpdateVirtualUsage(ProcessBranch processBranch) {
        if (processBranch.getProcessBranchId() == null) {
            //processBranch.setType("1");
            init(processBranch);
            this.processBranchMapper.insertProcessBranch(processBranch);
        } else {
            this.processBranchMapper.updateProcessBranch(processBranch);
        }
        if ("Y".equals(processBranch.getProcessEndFlag())) {
            ProcessBranch processBranchEnd = new ProcessBranch();
            processBranchEnd.setLevel(processBranch.getLevel());
            processBranchEnd.setSortNum(processBranch.getSortNum() + 1);
            if (processBranch.getParent() != null) {
                processBranchEnd.setProcessBranchCode(generateCode(processBranch.getParent().getProcessBranchCode(), processBranch.getParentProcessBranchId()));
            } else {
                processBranchEnd.setProcessBranchCode(generateCode("", processBranch.getParentProcessBranchId()));
            }
            processBranchEnd.setParentProcessBranchId(processBranch.getParentProcessBranchId());
            processBranchEnd.setInterfaceId(processBranch.getInterfaceId());
            processBranchEnd.setInterfaceType(processBranch.getInterfaceType());
            processBranchEnd.setType("3");

            ProcessBranchEnd end = new ProcessBranchEnd();
            end.setEndType("2");
            end.setEndWay("1");
            // 创建流程结束分支
            this.processBranchEndMapper.insertProcessBranchEnd(end);

            processBranchEnd.setEntityId(end.getBranchEndId());
            this.processBranchMapper.insertProcessBranch(processBranchEnd);

            end.setProcessBranchId(processBranchEnd.getProcessBranchId());
            end.setInterfaceId(processBranchEnd.getInterfaceId());
            this.processBranchEndMapper.updateProcessBranchEnd(end);
        }
    }

    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;

    private void init(ProcessBranch processBranch) {
        String parentCode = "";
        if (processBranch.getParentProcessBranchId() != null && processBranch.getParentProcessBranchId() != 0) {
            ProcessBranch parent = this.selectProcessBranchByProcessBranchId(processBranch.getParentProcessBranchId());
            processBranch.setLevel(parent.getLevel() + 1);
            processBranch.setInterfaceType(parent.getInterfaceType());
            parentCode = parent.getProcessBranchCode();
            processBranch.setParent(parent);
        } else {
            processBranch.setLevel(1);
            processBranch.setParentProcessBranchId(0L);
            InterfaceInfo interfaceInfo = this.interfaceInfoMapper.selectInterfaceInfoById(processBranch.getInterfaceId());
            if (interfaceInfo.getFbType().equals("3")) {
                processBranch.setInterfaceType("4");
            } else {
                processBranch.setInterfaceType(interfaceInfo.getFbType());
            }
        }

        if (processBranch.getPreProcessBranchId() == null) {
            if (processBranch.getSortNum() != null) {
                this.processBranchMapper.updateSortNum(processBranch.getLevel(), processBranch.getSortNum(), processBranch.getParentProcessBranchId(), processBranch.getInterfaceId());
            } else {
                int maxSort = this.processBranchMapper.selectMaxSortByLevel(processBranch.getLevel(), processBranch.getParentProcessBranchId(), processBranch.getInterfaceId());
                processBranch.setSortNum(maxSort + 1);
            }
        } else {
            ProcessBranch preProcessBranch = this.processBranchMapper.selectProcessBranchByProcessBranchId(processBranch.getPreProcessBranchId());
            int sortNum = 0;
            if (preProcessBranch.getType().equals("1")) {
                sortNum = preProcessBranch.getSortNum() + 1;
            } else {
                List<ProcessBranch> processBranches = this.processBranchMapper.selectProcessBranchListByLevelAndSortNum(preProcessBranch);
                if (CollectionUtil.isNotEmpty(processBranches)) {
                    ProcessBranch pre = null;
                    for (ProcessBranch branch : processBranches) {
                        if (branch.getConditionType().equals("elseif")) {
                            pre = branch;
                        } else {
                            break;
                        }
                    }
                    if (pre == null) {
                        sortNum = preProcessBranch.getSortNum() + 1;
                    } else {
                        sortNum = pre.getSortNum() + 1;
                    }
                } else {
                    sortNum = preProcessBranch.getSortNum() + 1;
                }
            }
            this.processBranchMapper.updateSortNum(processBranch.getLevel(), sortNum, processBranch.getParentProcessBranchId(), processBranch.getInterfaceId());
            processBranch.setSortNum(sortNum);
        }
        processBranch.setProcessBranchCode(generateCode(parentCode, processBranch.getParentProcessBranchId()));
    }

    private String generateCode(String parentCode, Long parentProcessBranchId) {
        String maxCode = this.processBranchMapper.getMaxCodeByParentId(parentProcessBranchId);
        // code = 父Code + 序列号
        if (maxCode == null) {
            return String.format("%s%s", parentCode, "000001");
        } else {
            maxCode = maxCode.substring(maxCode.length() - 6);
            int seq = Integer.parseInt(maxCode) + 1;
            return String.format("%s%06d", parentCode, seq);
        }
    }

    @Resource
    private ProcessBranchConditionMapper processBranchConditionMapper;
    @Resource
    private ProcessBranchConditionDetailMapper processBranchConditionDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void saveOrUpdateCondition(ProcessBranch processBranch) {
        String processEndFlag = processBranch.getProcessEndFlag();
        ProcessBranchCondition processBranchCondition = processBranch.getProcessBranchCondition();
        String joinCondition1 = processBranchCondition.getJoinCondition();
        String branchConditionDesc = processBranchCondition.getBranchConditionDesc();
        List<ProcessBranchConditionDetail> processBranchConditionDetailList = processBranchCondition.getProcessBranchConditionDetailList();
        if (processBranch.getProcessBranchId() == null) {
            processBranch.setType("2");
            init(processBranch);
            // 保存条件分支
            processBranchCondition.setInterfaceId(processBranch.getInterfaceId());
            this.processBranchConditionMapper.insertProcessBranchCondition(processBranchCondition);
            processBranch.setEntityId(processBranchCondition.getBranchConditionId());

            this.processBranchMapper.insertProcessBranch(processBranch);
            processBranchCondition.setProcessBranchId(processBranch.getProcessBranchId());
        } else {
            processBranch = this.processBranchMapper.selectProcessBranchByProcessBranchId(processBranch.getProcessBranchId());
            processBranchCondition = this.processBranchConditionMapper.selectProcessBranchConditionByBranchConditionId(processBranch.getEntityId());
            processBranchCondition.setBranchConditionId(processBranch.getEntityId());
            processBranchCondition.setJoinCondition(joinCondition1);
            processBranchCondition.setBranchConditionDesc(branchConditionDesc);
            this.processBranchConditionDetailMapper.deleteByBranchConditionId(processBranchCondition.getBranchConditionId());
        }

        List<SysDictData> judge_condition = this.sysDictTypeService.selectDictDataByType("JUDGE_CONDITION");
        Map<String, List<SysDictData>> collect = judge_condition.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));

        String branchConditionName = "";
        String joinCondition = " " + processBranchCondition.getJoinCondition() + " ";
        if (CollectionUtil.isNotEmpty(processBranchConditionDetailList)) {
            Map<String, List<ProcessBranchConditionDetail>> record = new TreeMap<>();
            for (ProcessBranchConditionDetail processBranchConditionDetail : processBranchConditionDetailList) {
                List<ProcessBranchConditionDetail> processBranchConditionDetails = record.computeIfAbsent(processBranchConditionDetail.getBranchConditionGroupId(), key -> new ArrayList<>());
                processBranchConditionDetails.add(processBranchConditionDetail);
                processBranchConditionDetail.setBranchConditionId(processBranchCondition.getBranchConditionId());
                processBranchConditionDetail.setProcessBranchId(processBranch.getProcessBranchId());
                processBranchConditionDetail.setBranchInterfaceId(processBranch.getInterfaceId());

                if ("IS_NULL".equals(processBranchConditionDetail.getJudgeConditionDictCode()) || "IS_NOT_NULL".equals(processBranchConditionDetail.getJudgeConditionDictCode())) {
                    processBranchConditionDetail.setContractValue(null);
                }
            }
            for (Map.Entry<String, List<ProcessBranchConditionDetail>> stringListEntry : record.entrySet()) {
                List<ProcessBranchConditionDetail> details = stringListEntry.getValue();
                String conditionName = "";
                for (int i = 0; i < details.size(); i++) {
                    ProcessBranchConditionDetail detail = details.get(i);
                    if (detail.getBranchConditionDetailDictCode().equals("3")) {
                        conditionName = conditionName + detail.getInterfaceName() + ".出参";
                    } else {
                        conditionName = conditionName + detail.getInterfaceName() + "." + detail.getObjectName();
                        if (detail.getBranchConditionDetailDictCode().equals("1")) {
                            if (StringUtils.isNotEmpty(detail.getObjectFieldName())) {
                                conditionName = conditionName + "." + detail.getObjectFieldName();
                            }
                        } else {
                            conditionName = conditionName + "数据集条数";
                        }
                    }
                    if ("IS_NULL".equals(detail.getJudgeConditionDictCode())) {
                        conditionName = conditionName + convertDict("IS_NULL", collect);
                    } else if ("IS_NOT_NULL".equals(detail.getJudgeConditionDictCode())) {
                        conditionName = conditionName + convertDict("IS_NOT_NULL", collect);
                    } else if ("IS_TRUE".equals(detail.getJudgeConditionDictCode())) {
                        conditionName = conditionName + convertDict("IS_TRUE", collect);
                    } else if ("IS_FALSE".equals(detail.getJudgeConditionDictCode())) {
                        conditionName = conditionName + convertDict("IS_FALSE", collect);
                    } else {
                        conditionName = conditionName + convertDict(detail.getJudgeConditionDictCode(), collect) + (StringUtils.isNotEmpty(detail.getDictName()) ? detail.getDictName() : detail.getContractValue());
                    }
                    if (i == details.size() - 1) continue;
                    conditionName = conditionName + " " + detail.getJoinCondition() + " ";
                }

                if (details.size() > 1) {
                    conditionName = "(" + conditionName + ")";
                }
                branchConditionName = branchConditionName + conditionName + joinCondition;
            }
            this.processBranchConditionDetailMapper.batchSave(processBranchConditionDetailList);
        }

        if (processBranchCondition.getType().equals("else")) {
            if (!StringUtils.isNotEmpty(processBranchCondition.getBranchConditionDesc())) {
                processBranchCondition.setBranchConditionDesc("否则");
            }
            processBranchCondition.setBranchConditionName("else");
        } else {
            String substring = branchConditionName.substring(0, branchConditionName.length() - joinCondition.length());
            processBranchCondition.setBranchConditionName(processBranchCondition.getType() +
                    "(" + substring + ")");
            //if (!StringUtils.isNotEmpty(processBranchCondition.getBranchConditionDesc())) {
                processBranchCondition.setBranchConditionDesc("如果" + substring + " 执行下一步");
            //}
        }
        this.processBranchConditionMapper.updateProcessBranchCondition(processBranchCondition);

        if ("Y".equals(processEndFlag)) {
            if (processBranchCondition.getType().equals("else")) {
                // 创建异常结束
                ProcessBranch processBranchEnd = new ProcessBranch();
                processBranchEnd.setLevel(processBranch.getLevel() + 1);
                processBranchEnd.setSortNum(1);
                processBranchEnd.setProcessBranchCode(generateCode(processBranch.getProcessBranchCode(), processBranch.getProcessBranchId()));
                processBranchEnd.setParentProcessBranchId(processBranch.getProcessBranchId());
                processBranchEnd.setInterfaceId(processBranch.getInterfaceId());
                processBranchEnd.setInterfaceType(processBranch.getInterfaceType());
                processBranchEnd.setType("3");
                ProcessBranchEnd end = new ProcessBranchEnd();
                end.setEndType("2");
                end.setEndWay("2");
                // 创建流程结束分支
                this.processBranchEndMapper.insertProcessBranchEnd(end);

                processBranchEnd.setEntityId(end.getBranchEndId());
                this.processBranchMapper.insertProcessBranch(processBranchEnd);

                end.setProcessBranchId(processBranchEnd.getProcessBranchId());
                end.setInterfaceId(processBranchEnd.getInterfaceId());
                this.processBranchEndMapper.updateProcessBranchEnd(end);
            } else {
                // 创建 else
                ProcessBranch processBranchElse = new ProcessBranch();
                processBranchElse.setParentProcessBranchId(processBranch.getParentProcessBranchId());
                processBranchElse.setType("2");
                processBranchElse.setInterfaceId(processBranch.getInterfaceId());
                processBranchElse.setProcessEndFlag("Y");
                ProcessBranchCondition processBranchConditionElse = new ProcessBranchCondition();
                processBranchConditionElse.setType("else");
                processBranchElse.setProcessBranchCondition(processBranchConditionElse);
                processBranchElse.setSortNum(processBranch.getSortNum() + 1);
                this.saveOrUpdateCondition(processBranchElse);
            }
        }
    }

    public static String convertDict(String value, Map<String, List<SysDictData>> collect) {
        List<SysDictData> sysDictData = collect.get(value);
        if (CollectionUtil.isNotEmpty(sysDictData)) {
            return " " + sysDictData.get(0).getDictLabel() + " ";
        }
        return "";
    }

    @Override
    public List<VirtualUsage> selectVirtualUsageIdsByProcessBranch(ProcessBranch processBranch) {
        List<VirtualUsage> result = new ArrayList<>();
        while (true) {
            List<VirtualUsage> ids = this.processBranchMapper.selectVirtualUsageIdsBySortNumAndParentId(processBranch.getSortNum(), processBranch.getParentProcessBranchId(), processBranch.getInterfaceId());
            if (CollectionUtil.isNotEmpty(ids)) {
                result.addAll(ids);
            }
            if (processBranch.getLevel() == 1) {
                break;
            }
            processBranch = this.processBranchMapper.selectProcessBranchByProcessBranchId(processBranch.getParentProcessBranchId());
        }
        return result;
    }

    @Override
    public List<ParamObject> paramObjectList(String interfaceType, List<Long> interfaceIds, String isInParam, String objectType) {
        return this.processBranchMapper.paramObjectList(interfaceType, interfaceIds, isInParam, objectType);
    }

    @Override
    public List<ParamObject> queryParamFieldList(Long id) {
        return this.processBranchMapper.queryParamFieldList(id);
    }

    @Override
    public List<ProcessBranch> selectProcessBranchListByInterfaceId(Long interfaceId) {
        return null;
    }

    @Override
    public void saveEnd(ProcessBranch processBranch) {
        processBranch.setType("3");
        init(processBranch);

        ProcessBranchEnd end = new ProcessBranchEnd();
        end.setEndType("2");
        end.setEndWay("1");
        // 创建流程结束分支
        this.processBranchEndMapper.insertProcessBranchEnd(end);

        processBranch.setEntityId(end.getBranchEndId());
        this.processBranchMapper.insertProcessBranch(processBranch);

        end.setProcessBranchId(processBranch.getProcessBranchId());
        end.setInterfaceId(processBranch.getInterfaceId());
        this.processBranchEndMapper.updateProcessBranchEnd(end);
    }

    @Override
    @Transactional
    public int delete(Long processBranchId) {
        ProcessBranch processBranch = this.processBranchMapper.selectProcessBranchByProcessBranchId(processBranchId);
        List<ProcessBranch> processBranches = this.processBranchMapper.selectProcessBranchByProcessBranchCode(processBranch.getProcessBranchCode());
        if (CollectionUtil.isNotEmpty(processBranches)) {
            for (ProcessBranch branch : processBranches) {
                if ("1".equals(branch.getType())) {
                    ProcessBranchConditionDetail processBranchConditionDetail = new ProcessBranchConditionDetail();
                    processBranchConditionDetail.setInterfaceId(branch.getEntityId());
                    processBranchConditionDetail.setObjectProcessBranchId(branch.getProcessBranchId());
                    List<ProcessBranchConditionDetail> processBranchConditionDetails = this.processBranchConditionDetailMapper.selectProcessBranchConditionDetailList(processBranchConditionDetail);
                    if (CollectionUtil.isNotEmpty(processBranchConditionDetails)) {
                        ProcessBranchCondition processBranchCondition = this.processBranchConditionMapper.selectProcessBranchConditionByBranchConditionId(processBranchConditionDetails.get(0).getBranchConditionId());
                        if (processBranchCondition != null) {
                            throw new BusinessException("被分支（" + processBranchCondition.getBranchConditionName() + "）引用，不能删除");
                        }
                    }

                    VirtualUsageParamSource virtualUsageParamSource = new VirtualUsageParamSource();
                    virtualUsageParamSource.setSourceProcessBranchId(branch.getProcessBranchId());
                    List<VirtualUsageParamSource> virtualUsageParamSourceList = this.virtualUsageParamSourceMapper.selectVirtualUsageParamSourceList(virtualUsageParamSource);
                    if (CollectionUtil.isNotEmpty(virtualUsageParamSourceList)) {
                        for (VirtualUsageParamSource paramSource : virtualUsageParamSourceList) {
                            VirtualUsage virtualUsage = new VirtualUsage();
                            virtualUsage.setId(paramSource.getInterfaceId());
                            List<VirtualUsage> virtualUsages = this.virtualUsageMapper.selectVirtualUsages(virtualUsage);
                            if (CollectionUtil.isNotEmpty(virtualUsages)) {
                                throw new BusinessException("被分支（" + virtualUsages.get(0).getSlaveInterfaceName() + "）引用，不能删除");
                            }
                        }
                    }
                }
            }
            List<Long> processBranchIds = processBranches.stream().map(ProcessBranch::getProcessBranchId).collect(Collectors.toList());
            this.processBranchMapper.batchDelete(processBranchIds);
            this.processBranchConditionMapper.batchDeleteByProcessBranchIds(processBranchIds);
            this.processBranchConditionDetailMapper.batchDeleteByProcessBranchIds(processBranchIds);
            this.processBranchEndMapper.batchDeleteByProcessBranchIds(processBranchIds);
            this.virtualUsageParamSourceMapper.batchDeleteByProcessBranchIds(processBranchIds);
            this.virtualUsageParamSourceMapper.batchDeleteBySourceProcessBranchIds(processBranchIds);
        }
        return 1;
    }

    @Override
    public void saveExceptionEnd(ProcessBranch processBranch) {
        processBranch.setType("3");
        init(processBranch);
        ProcessBranchEnd end = new ProcessBranchEnd();
        end.setEndType("2");
        end.setEndWay("2");
        // 创建流程结束分支
        this.processBranchEndMapper.insertProcessBranchEnd(end);

        processBranch.setEntityId(end.getBranchEndId());
        this.processBranchMapper.insertProcessBranch(processBranch);

        end.setProcessBranchId(processBranch.getProcessBranchId());
        end.setInterfaceId(processBranch.getInterfaceId());
        this.processBranchEndMapper.updateProcessBranchEnd(end);
    }

    @Override
    public List<VirtualUsage> selectVirtualUsageIdsByLevel(long level, Long interfaceId) {
        return this.processBranchMapper.selectVirtualUsageIdsByLevel(level, interfaceId);
    }

    @Override
    public void saveBreakEnd(ProcessBranch processBranch) {
        processBranch.setType("3");
        init(processBranch);
        ProcessBranchEnd end = new ProcessBranchEnd();
        end.setEndType("2");
        end.setEndWay("3");
        // 创建流程结束分支
        this.processBranchEndMapper.insertProcessBranchEnd(end);

        processBranch.setEntityId(end.getBranchEndId());
        this.processBranchMapper.insertProcessBranch(processBranch);

        end.setProcessBranchId(processBranch.getProcessBranchId());
        end.setInterfaceId(processBranch.getInterfaceId());
        this.processBranchEndMapper.updateProcessBranchEnd(end);
    }

    @Override
    public void saveContinueEnd(ProcessBranch processBranch) {
        processBranch.setType("3");
        init(processBranch);
        ProcessBranchEnd end = new ProcessBranchEnd();
        end.setEndType("2");
        end.setEndWay("4");
        // 创建流程结束分支
        this.processBranchEndMapper.insertProcessBranchEnd(end);

        processBranch.setEntityId(end.getBranchEndId());
        this.processBranchMapper.insertProcessBranch(processBranch);

        end.setProcessBranchId(processBranch.getProcessBranchId());
        end.setInterfaceId(processBranch.getInterfaceId());
        this.processBranchEndMapper.updateProcessBranchEnd(end);
    }
}
